// 集群

const cluster = require("cluster");
const os = require("os");


const cpuCount = os.cpus().length;  
console.log(`CPU总数为 ${cpuCount}`); 
console.log(`Primary pid=${process.pid}`); 

cluster.setupPrimary({ exec: __dirname + "/app.js", });  

for (let i = 0; i < cpuCount; i++) {   
    cluster.fork(); 
} 

// 监听线程是否挂掉
cluster.on("exit", (worker, code, signal) => {   
    console.log(`worker ${worker.process.pid} 已被杀死`);  
    console.log("启动另一个工人");   
    cluster.fork(); 
});